/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package samoloty.model.models;

import java.util.ArrayList;

/**
 *
 * @author borysfan
 */
/*
 * StabTrimSubSub ma w sobie dwie zmienne
 * StabTrimSub ma w sobie listę StabTrimSubSub + tow + display
 * StabTrim ma w sobie listę StabTrimSub + operację do szukania stab
 *
 */
public class StabTrimModel {

    private ArrayList<StabTrimSubModel> stabTrimSubModels;

    public StabTrimModel(ArrayList<StabTrimSubModel> stabTrimSubModels) {
        this.stabTrimSubModels = stabTrimSubModels;
    }

    public void setStabTrimSubModel(ArrayList<StabTrimSubModel> stabTrimSubModels) {
        this.stabTrimSubModels = stabTrimSubModels;
    }

    public ArrayList<StabTrimSubModel> getStabTrimSubModel() {
        return stabTrimSubModels;
    }

    public double findStab(int tow, double mac) throws IndexOutOfBoundsException {
        int i, j, k = 0;
        boolean iFlag=false, jFlag=false, kFlag = false;
        //iteracja w pionie
        for (i = 0; i < stabTrimSubModels.size(); i++) {
            if (stabTrimSubModels.get(i).getTow() > tow) {
                iFlag = true;
                break;
            }
        }
        if (!iFlag) {
            return 0;
        }
        int lowMac1, lowMac2 = 0;
        double lowStab1, lowStab2 = 0;
        double lowAvg = 0;
        //pierwsza iteracja w poziomie
        ArrayList<StabTrimSubSubModel> subLow = stabTrimSubModels.get(i - 1).getStabMacs();
        for (j = 0; j < subLow.size(); j++) {
            if (subLow.get(j).getMac() > mac) {
                jFlag = true;
                break;
            }
        }
        if (!jFlag) {
            return 0;
        }
        lowMac1 = subLow.get(j - 1).getMac();
        lowMac2 = subLow.get(j).getMac();
        lowStab1 = subLow.get(j - 1).getStab();
        lowStab2 = subLow.get(j).getStab();
        lowAvg = (lowStab2 - lowStab1) * (mac - lowMac1) / (lowMac2 - lowMac1) + lowStab1;
        
        int highMac1, highMac2 = 0;
        double highStab1, highStab2, highAvg = 0;

        //druga iteracja w poziomie
        ArrayList<StabTrimSubSubModel> subHigh = stabTrimSubModels.get(i).getStabMacs();
        for (k = 0; k < subHigh.size(); k++) {
            if (subHigh.get(k).getMac() > mac) {
                kFlag = true;
                break;
            }
        }
        if (!kFlag) {
            return 0;
        }
        highMac1 = subHigh.get(k - 1).getMac();
        highMac2 = subHigh.get(k).getMac();
        highStab1 = subHigh.get(k - 1).getStab();
        highStab2 = subHigh.get(k).getStab();

        highAvg = (highStab2 - highStab1) * (mac - highMac1) / (highMac2 - highMac1) + highStab1;

        //średnia ze średniej, czyli aproksymacja aproksymacji
        //return ((subLow.get(j - 1).getStab() + subLow.get(j).getStab()) / 2 + (subHigh.get(k - 1).getStab() + subHigh.get(k).getStab()) / 2) / 2;
        
        return (lowAvg + highAvg) / 2;
    }
}
